import java.util.*;


public class snippet {
    public ArrayList<Integer> intersection (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < nums1.size(); i++) {
            if (nums2.contains(nums1.get(i))) {
                //删除重复值
                while (nums2.remove(nums1.get(i))) ;
                list.add(nums1.get(i));
            }
        }
        return list;
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param nums1 int整型ArrayList
     * @param nums2 int整型ArrayList
     * @return int整型ArrayList
     */
    public ArrayList<Integer> intersection2 (ArrayList<Integer> nums1, ArrayList<Integer> nums2) {
        // write code here
        ArrayList<Integer> res = new ArrayList<>();
        HashSet<Integer> hashSet = new HashSet<>();
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums1.size(); i++) {
            int num = hashMap.getOrDefault(nums1.get(i), 0);
            num++;
            hashMap.put(nums1.get(i), num);
        }
        for (int i = 0; i < nums2.size(); i++) {
            int num = hashMap.getOrDefault(nums2.get(i), 0);
            if (num != 0) {
                hashSet.add(nums2.get(i));
            }
        }
        res.addAll(hashSet);
        return res;
    }
}